{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c2d4778b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.21.0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "print(np.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "9348032b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：<class 'numpy.ndarray'>\n",
      "<class 'numpy.ndarray'>\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "转置数组：\n",
      "<class 'numpy.ndarray'>\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "NumPy 中除了可以使用 numpy.transpose 函数来对换数组的维度，还可以使用 T 属性。。\n",
    "\n",
    "例如有个 m 行 n 列的矩阵，使用 t() 函数就能转换为 n 行 m 列的矩阵。\n",
    "\"\"\"\n",
    "\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "\n",
    "print('原数组：'+repr(type(a)))\n",
    "print(type(a))\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "\n",
    "print('转置数组：')\n",
    "print(type(a))\n",
    "print(a.T)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "795f2c70",
   "metadata": {},
   "source": [
    "### 矩阵相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9683cfc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0],\n",
       "       [0, 1]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 = np.array(\n",
    "    [\n",
    "        [1, 0],\n",
    "        [0, -1]\n",
    "    ]\n",
    ")\n",
    "\n",
    "a2 = np.array(\n",
    "    [\n",
    "        [1, 0],\n",
    "        [0, -1]\n",
    "    ]\n",
    ")\n",
    "\n",
    "\n",
    "np.dot(a1, a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "df0332a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "130\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2],[3,4]]) \n",
    "b = np.array([[11,12],[13,14]]) \n",
    " \n",
    "# vdot 将数组展开计算内积\n",
    "print (np.vdot(a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb0f17d2",
   "metadata": {},
   "source": [
    "### numpy.inner()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "00bcacc5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "numpy.inner() 函数返回一维数组的向量内积。对于更高的维度，它返回最后一个轴上的和的乘积。\n",
    "\"\"\"\n",
    "print (np.inner(np.array([1,2,3]),np.array([0,1,0])))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3347ae1",
   "metadata": {},
   "source": [
    "### 逆矩阵 numpy.linalg.inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f0a03a27",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]]\n",
      "[[-2.   1. ]\n",
      " [ 1.5 -0.5]]\n",
      "[[4 6]\n",
      " [4 6]]\n",
      "[[1. 1.]\n",
      " [1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[1, 1], [1, 1]])\n",
    "\n",
    "A = np.array([[1, 2], [3, 4]])\n",
    "#  获得A的逆矩阵\n",
    "B = np.linalg.inv(A)\n",
    "\n",
    "print(A)\n",
    "\n",
    "print(B)\n",
    "\n",
    "print(np.dot(x, A))\n",
    "\n",
    "print(np.dot(np.dot(x, A), B))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
